//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//import android.util.Log;

public class Config extends Activity {
	
	private WidgetInfo wInfo = null;
    	
    public Config() {
        super();
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent i) {
    	if (requestCode == 2)
    		finish();
    	
    	if (requestCode > 0 && resultCode == RESULT_OK)
    		finish();
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Find widget id
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
        	wInfo = new WidgetInfo(intent);
        }
        if (wInfo.getID() == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
        
        // Unlock all widgets
        //Intent widgetUnlockIntent = new Intent();
        //widgetUnlockIntent.setAction(TokaplotPlusService.ACTION_APPWIDGET_UNLOCK);
    	//sendBroadcast(widgetUnlockIntent);
        
        SharedPreferences mPrefs = getSharedPreferences("TP", Context.MODE_PRIVATE);
        boolean isPaused = mPrefs.getBoolean("isPaused", false);    	

        //  Setup view
        setContentView(R.layout.config);
        
        findViewById(R.id.config_button_plot).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(Config.this, ConfigPlot.class);
            	wInfo.setIntentWithInfo(i);
            	startActivityForResult(i, 1);
            }
        });
        
        findViewById(R.id.config_button_graphics).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(Config.this, ConfigGraphics.class);
            	wInfo.setIntentWithInfo(i);
            	startActivityForResult(i, 1);
            }
        });
        
        findViewById(R.id.config_button_help).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(Config.this, Help.class);
            	startActivity(i);
            }
        });
        
        findViewById(R.id.config_button_diagnostics).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(Config.this, Diagnostic.class);
            	wInfo.setIntentWithInfo(i);
            	startActivityForResult(i, 1);
            }
        });
        
        if (isPaused){
        	Drawable img = getResources().getDrawable(R.drawable.ciconm);
        	((Button)findViewById(R.id.config_button_pause)).setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);
        	((TextView)findViewById(R.id.config_button_pause)).setText("Resume plot");
            findViewById(R.id.config_button_pause).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    SharedPreferences mPausePrefs = getSharedPreferences("TP", Context.MODE_PRIVATE);
                    SharedPreferences.Editor ed = mPausePrefs.edit();
                    ed.putBoolean("isPaused", false);
                    ed.commit();
                    
                    Intent widgetPauseIntent = new Intent();
                    widgetPauseIntent.setAction(TokaplotService.ACTION_APPWIDGET_RESUME);
                	sendBroadcast(widgetPauseIntent);
                    
                    finish();
                }
            });
        } else {
        	Drawable img = getResources().getDrawable(R.drawable.ciconu);
        	((Button)findViewById(R.id.config_button_pause)).setCompoundDrawablesWithIntrinsicBounds(img, null, null, null);        	
        	((TextView)findViewById(R.id.config_button_pause)).setText("Pause plot");
            findViewById(R.id.config_button_pause).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    SharedPreferences mPausePrefs = getSharedPreferences("TP", Context.MODE_PRIVATE);
                    SharedPreferences.Editor ed = mPausePrefs.edit();
                    ed.putBoolean("isPaused", true);
                    ed.commit();
                    
                    Intent widgetPauseIntent = new Intent();
                    widgetPauseIntent.setAction(TokaplotService.ACTION_APPWIDGET_PAUSE);
                	sendBroadcast(widgetPauseIntent);
                    
                    finish();
                }
            });
        }
        
        findViewById(R.id.config_button_cancel).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }
}
